package com.sinoglobal.ningxia.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.amusement.BusinessDetailActivity;
import com.sinoglobal.ningxia.http.ConnectionUtil;
import com.sinoglobal.ningxia.utils.BitmapUtiles;
import com.sinoglobal.ningxia.utils.ItktLog;
import com.sinoglobal.ningxia.utils.TextUtil;

import net.tsz.afinal.FinalBitmap;

import java.io.IOException;
import java.util.ArrayList;

/**
 * 
 * @author hxp
 * @createdate 2014-6-19 下午5:35:27
 * @Description:商家详情图片适配器
 */
public class PhotoAdapter extends BaseAdapter {
	private BusinessDetailActivity context;// 上下文
	private ArrayList<String> infoes;// 照片信息集合
	private LayoutInflater layoutInflater;// 布局解析器
	private int tag;// 区分是那个布局调用的
	public final static int TAG_BUSINESS_PHOTO = 0;// 商家详情照片
	public final static int TAG_UPLOAD = 1;// 用户评论图片上传
	public final static int TAG_SHOW = 2;// 用户评论图片展示
	private FinalBitmap afinal;
	private Bitmap defuPic;

	public PhotoAdapter(BusinessDetailActivity context, ArrayList<String> infoes, int tag) {
		this.context = context;
		this.infoes = infoes;
		this.tag = tag;
		layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		defuPic = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
		afinal = FinalBitmap.create(context);
		afinal.configLoadfailImage(R.drawable.default43);
		afinal.configLoadingImage(R.drawable.default43);
	}

	public void setData(ArrayList<String> infoes) {
		this.infoes = infoes;
		this.notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return infoes.size();
	}

	@Override
	public Object getItem(int position) {
		return infoes.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		Com com;
		// 初始化item
		if (null == convertView) {
			com = new Com();
			convertView = layoutInflater.inflate(R.layout.business_img_item, null);
			com.img = (ImageView) convertView.findViewById(R.id.icon_img);
			convertView.setTag(com);
		} else {
			com = (Com) convertView.getTag();
		}
		switch (tag) {
		case TAG_BUSINESS_PHOTO:

			if (TextUtil.stringIsNotNull(infoes.get(position))) {
				com.img.setVisibility(View.VISIBLE);
				afinal.display(com.img, ConnectionUtil.localUrl + infoes.get(position));
			} else {
				com.img.setVisibility(View.INVISIBLE);
			}
			break;

		case TAG_UPLOAD:
			if ("addIcon".equals(infoes.get(position))) {
				com.img.setImageDrawable(null);
				com.img.setBackgroundResource(R.drawable.photo_add_bg);
			}

			if (infoes.get(position).indexOf("/") == -1) {
				// 资源图片
				try {
					int res = Integer.parseInt(infoes.get(position));
					com.img.setImageDrawable(context.getResources().getDrawable(res));
				} catch (Exception e) {
				}
			} else if (infoes.get(position).indexOf("http://") != -1) {
				afinal.display(com.img, ConnectionUtil.localUrl + infoes.get(position), 480, 800, defuPic, defuPic);
			} else {
				ItktLog.i("加载的相册图片!!");
				Bitmap drawable = null;
				try {
					drawable = BitmapUtiles.revitionImageSize(infoes.get(position));
				} catch (IOException e) {
					e.printStackTrace();
				}
				if (drawable != null) {
					com.img.setImageBitmap(drawable);
				} else {
					// 无图片时显示
					com.img.setImageResource(R.drawable.ic_launcher);
				}
			}
			break;

		case TAG_SHOW:
			if (position < 9) {
				if (TextUtil.stringIsNotNull(infoes.get(position))) {
					com.img.setVisibility(View.VISIBLE);
					afinal.display(com.img, ConnectionUtil.localUrl + infoes.get(position));
					// com.img.setOnClickListener(new OnClickListener() {
					//
					// @Override
					// public void onClick(View v) {
					// Intent intent = new Intent(context,
					// PhotoBigActivity.class);
					// intent.putExtra(PhotoBigActivity.PHOTOBIG, new
					// PhotoInfoVo(infoes.get(position), ""));
					// context.startActivity(intent);
					// }
					// });
				} else {
					com.img.setVisibility(View.INVISIBLE);
				}
			} else {
				com.img.setVisibility(View.GONE);
			}
			break;
		default:
			break;
		}
		return convertView;
	}

	/**
	 * 增加并改变视�?
	 * 
	 * @param position
	 *            the position
	 * @param imagePaths
	 *            the image paths
	 */
	public void addItem(int position, String imagePaths) {
		infoes.add(position, imagePaths);
		notifyDataSetChanged();
	}

	/**
	 * 增加多条并改变视�?
	 * 
	 * @param imagePaths
	 *            the image paths
	 */
	public void addItems(ArrayList<String> imagePaths) {
		infoes.addAll(imagePaths);
		notifyDataSetChanged();
	}

	/**
	 * 增加多条并改变视�?
	 */
	public void clearItems() {
		infoes.clear();
		notifyDataSetChanged();
	}

	/**
	 * item 重用辅助类
	 * 
	 * @author hxp
	 * @createdate 2014-6-23 下午4:59:37
	 * @Description:
	 */
	class Com {
		ImageView img;
	}
}
